package com.jf.blog_jf04.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jf.blog_jf04.bean.Blog;
import com.jf.blog_jf04.bean.Comment;
import com.jf.blog_jf04.service.*;
import com.jf.blog_jf04.utils.DateRangeUtils;
import com.jf.blog_jf04.utils.LoginUtils;
import com.jf.blog_jf04.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/timeline")
@Api(tags = "统计数据")
public class TimelineController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TagService tagService;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private TimelineService timelineService;

    @GetMapping("/echarts")
    @ApiOperation("获取图标数据")
    public R echarts(@ApiParam(value = "用户选择1全部2当前", allowableValues = "1,2", example = "1", defaultValue = "1") @RequestParam int userChange) {
        Integer userinfoId = null;
        if (userChange == 2) {
            userinfoId = LoginUtils.getLoginUserinfo().getUserinfoId();
        }
        return timelineService.getEcharts(userinfoId);
    }


    @GetMapping("/count")
    @ApiOperation("获取各模块总计")
    public R count(@ApiParam(value = "用户选择1全部2当前", allowableValues = "1,2", example = "1", defaultValue = "1") @RequestParam int userChange, @ApiParam("时间范围") @RequestParam int timeChange) {
        Map<String, Long> map = new HashMap<>();
        long blogCount = blogService.count(setQw(userChange, timeChange));
        long categoryCount = categoryService.count(setQw(userChange, timeChange));
        long tagCount = tagService.count(setQw(userChange, timeChange));
        if (userChange == 1) {
            long userinfoCount = userinfoService.count(setQw(userChange, timeChange));
            map.put("userinfoCount", userinfoCount);
        }


        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        String[] timeRange = getTimeRange(timeChange);
        if (timeRange != null) {
            queryWrapper.between("create_time", timeRange[0], timeRange[1]);
        }
        if (userChange != 1) {
            /*获取当前用户的所有博客编号*/
            QueryWrapper<Blog> blogQueryWrapper = new QueryWrapper<>();
            List<Blog> blogList = blogService.list(blogQueryWrapper.eq("create_user", LoginUtils.getLoginUserinfo().getUserinfoId()));
            if (!blogList.isEmpty()) {
                List<Integer> blogIdList = blogList.stream().map(Blog::getBlogId).collect(Collectors.toList());
                queryWrapper.in("blog_id", blogIdList);
            }
        }
        long commentCount = commentService.count(queryWrapper);

        map.put("blogCount", blogCount);
        map.put("commentCount", commentCount);
        map.put("categoryCount", categoryCount);
        map.put("tagCount", tagCount);
        return R.success(map);
    }

    private <T> QueryWrapper<T> setQw(int userChange, int timeChange) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        if (userChange != 1) {
            qw.eq("create_user", LoginUtils.getLoginUserinfo().getUserinfoId());
        }
        String[] timeRange = getTimeRange(timeChange);
        if (timeRange != null) {
            qw.between("create_time", timeRange[0], timeRange[1]);
        }
        return qw;
    }

    private String[] getTimeRange(int timeChange) {
        switch (timeChange) {
            case 2:
                return DateRangeUtils.getToday();
            case 3:
                return DateRangeUtils.getLastWeek();
            case 4:
                return DateRangeUtils.getLastMonth();
            case 5:
                return DateRangeUtils.getLastThreeMonths();
            case 6:
                return DateRangeUtils.getLastHalfYear();
            case 7:
                return DateRangeUtils.getLastYear();
            default:
                return null;
        }
    }
}
